package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.constants.SystemConstant;
import cn.stylefeng.guns.modular.gridsystem.entity.ThirdPartyInterconnection;
import cn.stylefeng.guns.modular.gridsystem.enums.LogExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.enums.SystemParamExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.mapper.ThirdPartyInterconnectionMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.ThirdPartyInterconnectionParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.ThirdPartyInterconnectionResult;
import cn.stylefeng.guns.modular.gridsystem.service.ThirdPartyInterconnectionService;
import cn.stylefeng.guns.modular.gridsystem.utils.OptionalBean;
import cn.stylefeng.guns.sys.modular.org.service.SysOrgService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 第三方对接 服务实现类
 *
 * @author ssy
 * @date 2021/11/04 17:42
 */
@Service
public class ThirdPartyInterconnectionServiceImpl extends ServiceImpl<ThirdPartyInterconnectionMapper, ThirdPartyInterconnection> implements ThirdPartyInterconnectionService {

    @Resource
    private SysOrgService sysOrgService;

    /**
     * 分页查询第三方对接
     *
     * @param thirdPartyInterconnectionParam orgId 所属组织
     * @return PageResult<ThirdPartyInterconnection>
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    public PageResult<ThirdPartyInterconnection> page(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        // 构造条件
        LambdaQueryWrapper<ThirdPartyInterconnection> queryWrapper = new LambdaQueryWrapper<>();
        // 拼接查询条件
        if (ObjectUtil.isNotNull(thirdPartyInterconnectionParam)) {
            if (ObjectUtil.isNotEmpty(thirdPartyInterconnectionParam.getOrgId())) {
                List<Long> childIdListWithSelfById = sysOrgService.getChildIdListWithSelfById(thirdPartyInterconnectionParam.getOrgId());
                queryWrapper.in(ThirdPartyInterconnection::getOrgId, childIdListWithSelfById);
            }
        }
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    /**
     * 查询所有第三方对接
     *
     * @param thirdPartyInterconnectionParam orgId 所属组织
     * @return List<ThirdPartyInterconnection>
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    public List<ThirdPartyInterconnection> list(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        // 构造条件
        LambdaQueryWrapper<ThirdPartyInterconnection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(thirdPartyInterconnectionParam.getOrgId()),
                ThirdPartyInterconnection::getOrgId, thirdPartyInterconnectionParam.getOrgId());
        return list(queryWrapper);
    }

    /**
     * 添加第三方对接
     *
     * @param thirdPartyInterconnectionParam thirdPartyUrl 第三方接口地址
     *                                       appKey 第三方标识
     *                                       businessSystemName 业务系统名称
     *                                       orgId 所属组织
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        //入参判空
        OptionalBean.ofNullable(thirdPartyInterconnectionParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        // 将dto转为实体
        ThirdPartyInterconnection thirdPartyInterconnection = new ThirdPartyInterconnection();
        BeanUtil.copyProperties(thirdPartyInterconnectionParam, thirdPartyInterconnection);
        thirdPartyInterconnection.setCreateTime(new Date());
        thirdPartyInterconnection.setCreateUser(LoginContextHolder.me().getSysLoginUser().getId());
        String thirdPartyUrl = thirdPartyInterconnectionParam.getThirdPartyUrl();
        Long orgId = thirdPartyInterconnectionParam.getOrgId();
        //sha1加密生成appKey
        String sha1Hex = DigestUtils.sha1Hex(new Date() + thirdPartyUrl + orgId);
        thirdPartyInterconnection.setAppKey(sha1Hex);
        save(thirdPartyInterconnection);
    }

    /**
     * 删除第三方对接
     *
     * @param thirdPartyInterconnectionParam id 主键id
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        removeById(thirdPartyInterconnectionParam.getId());
    }

    /**
     * 编辑第三方对接
     *
     * @param thirdPartyInterconnectionParam thirdPartyUrl 第三方接口地址
     *                                       appKey 第三方标识
     *                                       businessSystemName 业务系统名称
     *                                       orgId 所属组织
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        //入参判空
        OptionalBean.ofNullable(thirdPartyInterconnectionParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        ThirdPartyInterconnection thirdPartyInterconnection = new ThirdPartyInterconnection();
        // 请求参数转化为实体
        BeanUtil.copyProperties(thirdPartyInterconnectionParam, thirdPartyInterconnection);
        thirdPartyInterconnection.setUpdateUser(LoginContextHolder.me().getSysLoginUser().getId());
        thirdPartyInterconnection.setUpdateTime(new Date());
        updateById(thirdPartyInterconnection);
    }

    /**
     * 查看详情第三方对接
     *
     * @param thirdPartyInterconnectionParam 第三方对接
     * @return ThirdPartyInterconnectionResult
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    public ThirdPartyInterconnectionResult detail(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        return queryThirdPartyInterconnection(thirdPartyInterconnectionParam);
    }

    /**
     * 根据appKey 获取第三方站点信息
     *
     * @param appKey 第三方标识
     * @return ThirdPartyInterconnection
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    @Override
    public ThirdPartyInterconnection getByAppKey(String appKey) {
        //判断为空
        OptionalBean.ofStr(appKey);
        //根据appKey查询
        LambdaQueryWrapper<ThirdPartyInterconnection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ThirdPartyInterconnection::getAppKey, appKey);
        queryWrapper.last(SystemConstant.SQL_LIMIT);
        return getOne(queryWrapper);
    }


    /**
     * 根据id获取第三方对接信息
     *
     * @param thirdPartyInterconnectionParam id 主键id
     * @return ThirdPartyInterconnectionResult 第三方对接信息
     * @author 乔李莱莱
     * @since 2023/10/17
     */
    private ThirdPartyInterconnectionResult queryThirdPartyInterconnection(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        //入参判空
        OptionalBean.ofNullable(thirdPartyInterconnectionParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        OptionalBean.ofStr(thirdPartyInterconnectionParam.getId());
        //根据id查询第三方对接查询
        ThirdPartyInterconnection thirdPartyInterconnection = getById(thirdPartyInterconnectionParam.getId());
        OptionalBean.ofNullable(thirdPartyInterconnection).orElseThrow(() -> new ServiceException(LogExceptionEnum.NOT_EXISTED));
        ThirdPartyInterconnectionResult partyInterconnectionResult = new ThirdPartyInterconnectionResult();
        BeanUtil.copyProperties(thirdPartyInterconnection, partyInterconnectionResult);
        return partyInterconnectionResult;
    }
}
